package com.develop.data.loan.model;

import com.develop.data.base.model.IdSpaceTimeRecord;
import com.develop.data.loan.jpa.attribute.converter.LoanContractAttributeConverter;
import com.develop.data.loan.jpa.attribute.converter.LoanInstallmentTypeAttributeConverter;
import com.develop.data.loan.jpa.attribute.converter.LoanMoneyAmountAttributeConverter;
import com.develop.data.loan.option.LoanInstallmentType;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.javamoney.moneta.Money;

import javax.money.Monetary;
import javax.money.MonetaryAmount;
import javax.persistence.*;
import java.time.LocalDateTime;

@Entity
@Table(name = "LOAN_INSTALLMENT")
@SuperBuilder
@Getter
@Setter
@RequiredArgsConstructor
@AllArgsConstructor
@JsonIdentityInfo(
        generator = ObjectIdGenerators.PropertyGenerator.class,
        property = "id"
)
public class LoanInstallment extends IdSpaceTimeRecord {


    public String loanProductionNumber; //贷款产品编号，枚举类型合适
    public String loanBusinessNumber; //贷款业务编号，枚举类型合适

    @Convert(converter = LoanInstallmentTypeAttributeConverter.class)
    public LoanInstallmentType installmentType;
    /*分期期次*/
    public int installmentTerm;
    /*起息日*/
    public LocalDateTime installmentHead;
    /*还款日*/
    public LocalDateTime installmentTail;
    /*动帐日*/
    public LocalDateTime installmentCursor;
    /*期初应还本金*/
    @Builder.Default
    @Convert(converter = LoanMoneyAmountAttributeConverter.class)
    public MonetaryAmount contractPrincipalAccrualOpening = Money.zero(Monetary.getCurrency("CNY"));
    /*期末应还本金*/
    @Builder.Default
    @Convert(converter = LoanMoneyAmountAttributeConverter.class)
    public MonetaryAmount contractPrincipalAccrualClosing = Money.zero(Monetary.getCurrency("CNY"));
    /*到期应还本息*/
    @Builder.Default
    @Convert(converter = LoanMoneyAmountAttributeConverter.class)
    public MonetaryAmount installmentCorpusDueAccrual = Money.zero(Monetary.getCurrency("CNY"));

    /*到期应还利息*/
    @Builder.Default
    @Convert(converter = LoanMoneyAmountAttributeConverter.class)
    public MonetaryAmount installmentInterestDueAccrual = Money.zero(Monetary.getCurrency("CNY"));
    /*累计应还利息*/
    /*动帐日，每次动帐前，都会进行利息预算，持有本金×(逾期动帐日-上一动帐日)，累计该利息，确实持有本金这些日子了，即便立刻结清，这些利息也是要结清的*/
    @Builder.Default
    @Convert(converter = LoanMoneyAmountAttributeConverter.class)
    public MonetaryAmount installmentInterestAccrual = Money.zero(Monetary.getCurrency("CNY"));
    /*已结清利息*/
    @Builder.Default
    @Convert(converter = LoanMoneyAmountAttributeConverter.class)
    public MonetaryAmount installmentInterestCleared = Money.zero(Monetary.getCurrency("CNY"));
    /*累计逾期利息*/
    @Builder.Default
    @Convert(converter = LoanMoneyAmountAttributeConverter.class)
    public MonetaryAmount installmentInterestOverdue = Money.zero(Monetary.getCurrency("CNY"));
    /*已结清逾期利息*/
    @Builder.Default
    @Convert(converter = LoanMoneyAmountAttributeConverter.class)
    public MonetaryAmount installmentInterestOverdueCleared = Money.zero(Monetary.getCurrency("CNY"));
    /*累计利息罚息*/
    @Builder.Default
    @Convert(converter = LoanMoneyAmountAttributeConverter.class)
    public MonetaryAmount installmentInterestPenalty = Money.zero(Monetary.getCurrency("CNY"));
    /*已结清利息罚息*/
    @Builder.Default
    @Convert(converter = LoanMoneyAmountAttributeConverter.class)
    public MonetaryAmount installmentInterestPenaltyCleared = Money.zero(Monetary.getCurrency("CNY"));

    /*到期应还本金*/
    @Builder.Default
    @Convert(converter = LoanMoneyAmountAttributeConverter.class)
    public MonetaryAmount installmentPrincipalDueAccrual = Money.zero(Monetary.getCurrency("CNY"));
    /*已结清本金*/
    @Builder.Default
    @Convert(converter = LoanMoneyAmountAttributeConverter.class)
    public MonetaryAmount installmentPrincipalCleared = Money.zero(Monetary.getCurrency("CNY"));
    /*逾期本金*/
    @Builder.Default
    @Convert(converter = LoanMoneyAmountAttributeConverter.class)
    public MonetaryAmount installmentPrincipalOverdue = Money.zero(Monetary.getCurrency("CNY"));
    /*已结清逾期本金*/
    @Builder.Default
    @Convert(converter = LoanMoneyAmountAttributeConverter.class)
    public MonetaryAmount installmentPrincipalOverdueCleared = Money.zero(Monetary.getCurrency("CNY"));
    /*提前还款违约金*/
    @Builder.Default
    @Convert(converter = LoanMoneyAmountAttributeConverter.class)
    public MonetaryAmount installmentPenalty = Money.zero(Monetary.getCurrency("CNY"));
    /*已结请提前还款违约金*/
    @Builder.Default
    @Convert(converter = LoanMoneyAmountAttributeConverter.class)
    public MonetaryAmount installmentPenaltyCleared = Money.zero(Monetary.getCurrency("CNY"));


    @Builder.Default
    public boolean cleared=false;
    public int daysCleared(){return 0;}
    @Builder.Default
    public boolean overdue=false;
    @Builder.Default
    public boolean graced=false; /*宽限期*/
    public int daysOverdue(){return 0;}
    @Builder.Default
    public boolean accrual=true;
    public int daysNonAccrual(){return 0;}
    @Builder.Default
    public boolean cancel=false;
    public int daysCanceled(){return 0;}
    @Builder.Default
    public boolean expired=false;
    public int daysExpired(){return 0;}

    public LocalDateTime instantCleared;
    public LocalDateTime instantOverDue;
    public LocalDateTime instantFrozen;

    public LocalDateTime accountInstantNonAccrual ;
    public LocalDateTime accountInstantCancel;

    @ManyToOne
    @JoinColumn(foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
    @Convert(converter = LoanContractAttributeConverter.class)
    public LoanContract loanContract;

    @Override
    public String toString(){
        return id;
    }

}

